package com.zxw.hotel.api;

import com.github.pagehelper.PageInfo;
import com.zxw.hotel.common.Pageinfo;
import com.zxw.hotel.model.Admin;
import com.zxw.hotel.model.extend.AdminExtend;
import com.zxw.hotel.service.AdminService;
import org.jasypt.util.password.PasswordEncryptor;
import org.jasypt.util.password.StrongPasswordEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 实现对管理员的增删改查 分页操作
 */
@RestController
@RequestMapping(value = "/api/v1/admin", produces = "application/json;charset=utf-8")
@CacheConfig(cacheNames = "com.zxw.hotel.api.AdminApi")
public class AdminApi {
    private static final PasswordEncryptor encryptor = new StrongPasswordEncryptor();
    private AdminService adminService;
    @Autowired
    public AdminApi(AdminService adminService) {
        this.adminService = adminService;
    }
    //缓存
    @Cacheable
    //查询全部
    @GetMapping
    public Map<String, Object> getall(@RequestParam(defaultValue = "1") Integer pageNo,
                                      @RequestParam(defaultValue = "2147483647") Integer pageSize,
                                      AdminExtend adminExtend) {
        if (pageNo == null || pageNo < 1) {
            pageNo = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = Integer.MAX_VALUE;
        }

        Pageinfo pageinfo = new Pageinfo(pageNo, pageSize);
        List<Admin> alladmins = adminService.getAll(pageinfo, adminExtend);
        //通过分页插件获取分页信息
        PageInfo<Admin> pi = new PageInfo<>(alladmins);
        Map<String, Object> map = Map.of("total", pi.getTotal(), "pageNo", pi.getPageNum(), "pageSize", pi.getPageSize());
        return Map.of("success", true, "data", alladmins, "pi", map);
    }
    //删除
    @CacheEvict
    @DeleteMapping
    public ResponseEntity<Map<String, Object>> deleteByIds(@RequestBody Integer[] ids) {
        System.out.println(ids);
        int rows = adminService.deleteByIds(ids);
        return ResponseEntity.ok().body(Map.of("success", rows > 0, "rows", rows));
    }
    //修改
    @PutMapping
    @CacheEvict
    public ResponseEntity<Map<String, Object>> update(@RequestBody Admin admin) {
        if(admin.getBirthday()!=null){
            admin.setBirthday(admin.getBirthday().plusDays(1));
        }
        //加密
        if(admin.getPassword()!=null){
            admin.setPassword(encryptor.encryptPassword(admin.getPassword()));
        }
        boolean update = adminService.update(admin);
        return ResponseEntity.ok().body(Map.of("success", update));
    }
    //新增
    @PostMapping
    @CacheEvict
    public ResponseEntity<Map<String, Object>> insert(@RequestBody Admin admin) {
        //加密
        admin.setPassword(encryptor.encryptPassword(admin.getPassword()));
        //System.out.println(admin.toString());
        boolean add = adminService.add(admin);
        return ResponseEntity.ok().body(Map.of("success", add));
    }

}
